//
//  HSLotteryNumberDetailCell.m
//  HolyShit
//
//  Created by Robin on 2017/4/30.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSLotteryNumberDetailCell.h"

@implementation HSOptionListModel

@end

@implementation HSLotteryNumberDetailModel

- (instancetype)init
{
    if (self = [super init]) {
        _normalHeight = HLLotteryNumberTopHeight;
    }
    return self;
}

- (void)setOPEN_CODE:(NSString *)OPEN_CODE
{
    _OPEN_CODE = OPEN_CODE;
    NSArray<NSString *> *tmpStr = [OPEN_CODE componentsSeparatedByString:@"|"];
    if (tmpStr.count < 1) {
        return;
    }
    self.numbers = [tmpStr[0] componentsSeparatedByString:@","].mutableCopy;
    self.normalCount = self.numbers.count;
    if (tmpStr.count < 2) {
        return;
    }
    [self.numbers addObjectsFromArray:[tmpStr[1] componentsSeparatedByString:@","]];
}

- (void)setOPTIONS_LIST:(NSArray<HSOptionListModel *> *)OPTIONS_LIST
{
    _OPTIONS_LIST = OPTIONS_LIST;
    if (OPTIONS_LIST.count > 0) {
        _canExpend = YES;
        _expendHeight = _normalHeight + HLSubOneHeight + HLSubRowHeight * (OPTIONS_LIST.count + 1);
    }
}

+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass
{
    return @{@"OPTIONS_LIST" : [HSOptionListModel class]};
}

@end

@interface HSLotteryNumberDetailCell ()

@property (nonatomic, strong) UILabel *timeLb;
@property (nonatomic, strong) UILabel *numLb;
@property (nonatomic, strong) UIView *shishiView;
@property (nonatomic, strong) NSMutableArray<UILabel *> *subLbs;
@property (nonatomic, strong) UILabel *arrowLb;

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *subOneView;
@property (nonatomic, strong) UIView *subTwoView;
@property (nonatomic, strong) NSMutableArray<UIView *> *awardViews;
@property (nonatomic, strong) UILabel *sellLb;
@property (nonatomic, strong) UILabel *totalLb;

@end

@implementation HSLotteryNumberDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self commonInit];
    }
    return self;
}

static const CGFloat labelWid = 30;
static const CGFloat labelPad = 10;
static const NSUInteger HSLeftTag = 112233 + 'L';
static const NSUInteger HSMiddleTag = 112233 + 'M';
static const NSUInteger HSRightTag = 112233 + 'R';

- (void)commonInit
{
    self.clipsToBounds = YES;
    self.awardViews = @[].mutableCopy;
    self.subLbs = @[].mutableCopy;
    self.backgroundColor = HEXCOLOR(HSColorBgLight);
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.width = ScreenWidth;
    self.timeLb = [UILabel labelWithFont:MYFont(Font26) color:HEXCOLOR(HSColorGray)];
    [self addSubview:self.timeLb];
    self.numLb = [UILabel labelWithFont:MYFont(Font32) color:HEXCOLOR(HSColorGray)];
    [self addSubview:self.numLb];
    self.arrowLb = [UILabel labelWithFrame:CGRectZero font:IconFont(20) color:HEXCOLOR(HSColorGray) text:@""];
    [self addSubview:self.arrowLb];
    self.shishiView = [UIView new];
    self.shishiView.clipsToBounds = YES;
    [self addSubview:self.shishiView];
    
    [self.numLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(15);
    }];
    
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.numLb.mas_right).offset(5);
        make.bottom.equalTo(self.numLb);
    }];
    
    [self.shishiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.numLb);
        make.top.equalTo(self.numLb.mas_bottom).offset(15);
        make.height.mas_equalTo(labelWid);
    }];
    
    [self.arrowLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.equalTo(self.shishiView);
    }];
    
    self.bottomView = [UIView new];
    self.bottomView.clipsToBounds = YES;
    [self addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.shishiView.mas_bottom).offset(15);
    }];
    
    [self showLineForDirection:MYDirectionBottom];
    MYLine *line = [self lineWithDirection:MYDirectionBottom];
    [line manualSetBgColor:HEXCOLOR(HSColorRed)];
}

- (void)setModel:(HSLotteryNumberDetailModel *)model
{
    _model = model;
    if (model.canExpend) {
        self.arrowLb.hidden = NO;
        self.arrowLb.transform = CGAffineTransformMakeRotation(model.isExpend ? 180.1 *(M_PI/180.) : 0);
    }
    else{
        self.arrowLb.hidden = YES;
    }
    self.numLb.text = [NSString stringWithFormat:@"第%@期",model.EXPECT];
    self.timeLb.text = [NSString stringWithFormat:@"开奖时间%@(周%@)", model.OPENCODE_TIME,model.OPENCODE_WEEK];
    NSUInteger count = model.numbers.count;
    for (NSUInteger idx = 0 ; idx < count; ++idx) {
        UILabel *lb;
        if (idx >= self.subLbs.count) {
            lb = [self shiLabelWithFrame:CGRectMake( idx * (labelWid + labelPad), 0, labelWid, labelWid) title:model.numbers[idx]];
            [self.shishiView addSubview:lb];
            [self.subLbs addObject:lb];
        }
        else{
            lb = self.subLbs[idx];
            lb.text = model.numbers[idx];
        }
        lb.backgroundColor = idx < model.normalCount ? HEXCOLOR(HSColorRed) : HEXCOLOR(HSColorBlue);
    }
    [self.shishiView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(model.numbers.count * (labelWid + labelPad));
    }];
    if (model.OPTIONS_LIST.count > 0) {
        if (self.awardViews.count < 1) {
            //初始化
            [self.bottomView addSubview:self.subOneView];
            [self.subOneView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.mas_equalTo(0);
                make.height.mas_equalTo(HLSubOneHeight);
            }];
            
            UIView *topView = self.subOneView;
            [self.bottomView addSubview:self.subTwoView];
            [self.subTwoView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.top.mas_equalTo(topView.mas_bottom);
                make.height.mas_equalTo(HLSubRowHeight);
            }];
            topView = self.subTwoView;
            
            for (NSUInteger i = 0; i < model.OPTIONS_LIST.count; ++i) {
                UIView *view = [self awardViewWithIndex:i];
                [self.bottomView addSubview:view];
                [view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.right.mas_equalTo(0);
                    make.top.mas_equalTo(topView.mas_bottom);
                    make.height.mas_equalTo(HLSubRowHeight);
                }];
                topView = view;
                [self.awardViews addObject:view];
            }
            [self.bottomView showLineForDirection:MYDirectionTop];
            MYLine *line = [self.bottomView lineWithDirection:MYDirectionTop];
            [line manualSetBgColor:HEXCOLOR(HSColorRed)];
        }
        //更新数据
        NSString *sale = [NSString stringWithFormat:@"%@ 元",model.SALES_MONEY];
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:sale];
        [attr addAttributes:@{NSForegroundColorAttributeName : HEXCOLOR(HSColorBlack) , NSFontAttributeName : MYFont(Font24)} range:NSMakeRange(sale.length - 1, 1)];
        self.sellLb.attributedText = attr;
        NSString *total = [NSString stringWithFormat:@"%@ 元",model.PROGRESSIVE];
        attr = [[NSMutableAttributedString alloc] initWithString:total];
        [attr addAttributes:@{NSForegroundColorAttributeName : HEXCOLOR(HSColorBlack) , NSFontAttributeName : MYFont(Font24)} range:NSMakeRange(total.length - 1, 1)];
        self.totalLb.attributedText = attr;
        
        if (self.awardViews.count > 0) {
            [model.OPTIONS_LIST enumerateObjectsUsingBlock:^(HSOptionListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx > self.awardViews.count - 1) {
                    UIView *view = [self awardViewWithIndex:idx];
                    [self.bottomView addSubview:view];
                    [view mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.left.right.mas_equalTo(0);
                        make.top.mas_equalTo(self.awardViews.lastObject.mas_bottom);
                        make.height.mas_equalTo(HLSubRowHeight);
                    }];
                    [self.awardViews addObject:view];
                }
                UIView *view = self.awardViews[idx];
                UILabel *leftLb = [view viewWithTag:HSLeftTag];
                if (leftLb) {
                    leftLb.text = obj.award_level_name;
                }
                
                UILabel *middleLb = [view viewWithTag:HSMiddleTag];
                if (middleLb) {
                    middleLb.text = obj.award_number;
                }
                
                UILabel *rightLb = [view viewWithTag:HSRightTag];
                if (rightLb) {
                    rightLb.text = obj.each_bonus_money;
                }
            }];
        }
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(model.expendHeight);
        }];
    }
}

- (UILabel *)shiLabelWithFrame:(CGRect)frame title:(NSString *)title
{
    UILabel *lb = [UILabel labelWithFrame:frame font:MYFont(Font30) color:WHITE_COLOR text:title];
    lb.textAlignment = NSTextAlignmentCenter;
    lb.backgroundColor = [UIColor redColor];
    [lb makeRound:YES];
    return lb;
}

- (UIView *)awardViewWithIndex:(NSUInteger)index
{
    UIView *view = [UIView new];
    view.backgroundColor = index % 2 == 0 ? HEXCOLOR(HSColorSeparateLight) : WHITE_COLOR;
    
    UILabel *lb1 = [UILabel labelWithFont:MYFont(Font30) color:HEXCOLOR(HSColorBlack)];
    lb1.tag = HSLeftTag;
    lb1.textAlignment = NSTextAlignmentCenter;
    [view addSubview:lb1];
    [lb1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(ScreenWidth / 3.);
    }];
    
    UILabel *lb2 = [UILabel labelWithFont:MYFont(Font30) color:HEXCOLOR(HSColorBlack)];
    lb2.tag = HSMiddleTag;
    lb2.textAlignment = NSTextAlignmentCenter;
    [view addSubview:lb2];
    [lb2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(lb1.mas_right);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(ScreenWidth / 3.);
    }];
    
    UILabel *lb3 = [UILabel labelWithFont:MYFont(Font30) color:HEXCOLOR(HSColorRed)];
    lb3.tag = HSRightTag;
    lb3.textAlignment = NSTextAlignmentCenter;
    [view addSubview:lb3];
    [lb3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(lb2.mas_right);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(ScreenWidth / 3.);
    }];
    
    return view;
}

- (UIView *)subOneView
{
    if (!_subOneView) {
        _subOneView = [UIView new];
        _subOneView.backgroundColor = HEXCOLOR(HSColorSeparateLight);
        
        self.sellLb = [UILabel labelWithFont:MYFont(Font34) color:HEXCOLOR(HSColorRed)];
        self.sellLb.textAlignment = NSTextAlignmentCenter;
        [_subOneView addSubview:self.sellLb];
        [self.sellLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(10);
            make.width.mas_equalTo(ScreenWidth / 2.);
        }];
        
        self.totalLb = [UILabel labelWithFont:MYFont(Font34) color:HEXCOLOR(HSColorRed)];
        self.totalLb.textAlignment = NSTextAlignmentCenter;
        [_subOneView addSubview:self.totalLb];
        [self.totalLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.width.equalTo(self.sellLb);
            make.left.equalTo(self.sellLb.mas_right);
        }];
        
        UILabel *sellLb = [UILabel labelWithFont:MYFont(Font24) color:HEXCOLOR(HSColorBlack)];
        sellLb.text = @"本期销量";
        sellLb.textAlignment = NSTextAlignmentCenter;
        [_subOneView addSubview:sellLb];
        [sellLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.bottom.mas_equalTo(-10);
            make.width.mas_equalTo(ScreenWidth / 2.);
        }];
        
        UILabel *totalLb = [UILabel labelWithFont:MYFont(Font24) color:HEXCOLOR(HSColorBlack)];
        totalLb.text = @"奖池金额";
        totalLb.textAlignment = NSTextAlignmentCenter;
        [_subOneView addSubview:totalLb];
        [totalLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.width.equalTo(sellLb);
            make.left.equalTo(sellLb.mas_right);
        }];
    }
    return _subOneView;
}

- (UIView *)subTwoView{
    if(!_subTwoView){
        _subTwoView = [UIView new];
        _subTwoView.backgroundColor = WHITE_COLOR;
        
        UILabel *lb1 = [UILabel labelWithFont:MYFont(Font30) color:HEXCOLOR(HSColorGray)];
        lb1.text = @"奖项";
        lb1.textAlignment = NSTextAlignmentCenter;
        [_subTwoView addSubview:lb1];
        [lb1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(ScreenWidth / 3.);
        }];
        
        UILabel *lb2 = [UILabel labelWithFont:MYFont(Font30) color:HEXCOLOR(HSColorGray)];
        lb2.text = @"中奖注数";
        lb2.textAlignment = NSTextAlignmentCenter;
        [_subTwoView addSubview:lb2];
        [lb2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(lb1.mas_right);
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(ScreenWidth / 3.);
        }];
        
        UILabel *lb3 = [UILabel labelWithFont:MYFont(Font30) color:HEXCOLOR(HSColorGray)];
        lb3.text = @"每注金额(元)";
        lb3.textAlignment = NSTextAlignmentCenter;
        [_subTwoView addSubview:lb3];
        [lb3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(lb2.mas_right);
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(ScreenWidth / 3.);
        }];
        
    }
    return _subTwoView;
}

@end
